﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using CodeBox.Development.Tools.Foundation.Interfaces;
using CodeBox.Development.Tools.Foundation;
using CodeBox.Development.Tools.RepositoryBuilder.Engine.Interfaces;

namespace CodeBox.Development.Tools.RepositoryBuilder
{
    [Export("CodeBox.Development.Tools.RepositoryBuilderWindowContract")]
    [ExportProperty("WndContract", "default")]
    [CompositionOptions(CreationPolicy = CreationPolicy.Factory)]
    public partial class frmMain : Form, INotifyImport
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void mniFileExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        [Import]
        public ImportInfoCollection<IDirectoryStructureBuilder, IContractDescription> DirectoryBuilders { get; set; }
        [Import]
        public ImportInfoCollection<ISolutionBuilder, IContractDescription> SolutionBuilders { get; set; }
        [Import]
        public ImportInfoCollection<IRepositoryBuilder, IContractDescription> RepositoryBuilders { get; set; }
        [Import]
        public ImportInfoCollection<ITestingBuilder, IContractDescription> TestingBuilders { get; set; }
        [Import]
        public ImportInfoCollection<IBuildBuilder, IContractDescription> BuildBuilders { get; set; }
        [Import]
        public ImportInfoCollection<ICIBuilder, IContractDescription> CIBuilders { get; set; }

        private void RefreshDirectoryBuilders()
        {
            Refresher<IDirectoryStructureBuilder>(DirectoryBuilders, lstDirectoryBuilders);
        }

        private void RefreshSolutionBuilders()
        {
            Refresher<ISolutionBuilder>(SolutionBuilders, lstSolutions);
        }

        private void RefreshRepositoriesBuilders()
        {
            Refresher<IRepositoryBuilder>(RepositoryBuilders, lstRepositories);
        }

        private void RefreshTestingBuilders()
        {
            Refresher<ITestingBuilder>(TestingBuilders, lstTesting);
        }

        private void RefreshBuildBuilders()
        {
            Refresher<IBuildBuilder>(BuildBuilders, lstBuilders);
        }

        private void RefreshCIBuilders()
        {
            Refresher<ICIBuilder>(CIBuilders, lstCI);
        }

        private void Refresher<TBuilder>(ImportInfoCollection<TBuilder, IContractDescription> buildersList, ListBox listBox) where TBuilder : IRepositoryAction
        {
            UIInvoker(listBox, delegate() { listBox.Items.Clear(); listBox.Items.Add("(none)"); });
            foreach (ImportInfo<TBuilder, IContractDescription> item in buildersList
                .OrderBy(op => op.Metadata.Name))
            {
                UIInvoker(listBox, delegate() { listBox.Items.Add(item.Metadata.Name); });
            }
        }

        #region INotifyImport Members

        public void ImportCompleted()
        {
            RefreshDirectoryBuilders();
            RefreshSolutionBuilders();
            RefreshRepositoriesBuilders();
            RefreshTestingBuilders();
            RefreshBuildBuilders();
            RefreshCIBuilders();
        }

        #endregion
        
        private void UIInvoker(Control ctrl, Action del)
        {
            if (ctrl.InvokeRequired)
            {
                ctrl.BeginInvoke(new MethodInvoker(del));
            }
            else
            {
                del();
            }
        }

        private void btnBuildRepository_Click(object sender, EventArgs e)
        {
            if (lstDirectoryBuilders.SelectedIndex > 0)
            {
                IDirectoryStructureBuilder builder = DirectoryBuilders[lstDirectoryBuilders.SelectedIndex-1].GetBoundValue();
                builder.Name = txtProjectName.Text;
                builder.RootDirectory = txtProjectPath.Text;
                builder.Execute();
            }

            /// TODO: Replace Title with Project Name (see about form)
            MessageBox.Show("Done building!", "CodeBox Repository Builder", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        private void btnSearchDirectory_Click(object sender, EventArgs e)
        {
        }
    }
}
